using ProtoModels;
using UnityEngine;

public class FriendsButton : MonoBehaviour
{
	public new UILabel name;

	public UITexture texture;

	public UIToggle toggle;

	private FriendsDisplay friendsDisplay;

	private Texture2D myTexture;

	private int myGroupId = -1;

	private bool isFetchingTexture;

	public InvitableFriends.InvitableFriend friend { get; private set; }

	public void Init(InvitableFriends.InvitableFriend friend, int groupId, FriendsDisplay display)
	{
		this.friend = friend;
		friendsDisplay = display;
		UITools.ChangeText(name, friend.name);
		toggle.value = false;
		myGroupId = groupId;
		myTexture = null;
		isFetchingTexture = false;
	}

	private void OnEnable()
	{
		if (myTexture == null && friend != null && !isFetchingTexture)
		{
			GGServerRequestsBackend.TextureRequest textureRequest = new GGServerRequestsBackend.TextureRequest(friend.picture.data.url);
			textureRequest.groupId = myGroupId;
			textureRequest.onComplete = OnTextureFetchComplete;
			textureRequest.cache = CacheStategy.GetFromCache;
			BehaviourSingleton<GGRequestLimitter>.instance.Add(textureRequest);
			isFetchingTexture = true;
		}
	}

	public void OnTextureFetchComplete(GGServerRequestsBackend.ServerRequest request)
	{
		if (request.status == GGServerRequestsBackend.ServerRequest.RequestStatus.Success)
		{
			myTexture = request.GetResponse<Texture2D>();
			texture.mainTexture = myTexture;
			isFetchingTexture = false;
			friendsDisplay.OnTextureArrived();
		}
	}

	public bool IsToggleOn()
	{
		return toggle.value;
	}
}
